package com.edu.exam.entity.curriculum.classification;

import com.edu.exam.enums.core.EntityStatus;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.time.LocalDateTime;

/**
 * 题型分类实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：question_types
 *
 * 管理考试题目的类型分类，包含题型的层级关系、答题格式、默认分值等。
 * 支持题型的层级验证和规则查询。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "question_types")
@Comment("题型分类表")
public class QuestionType {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 题型代码，唯一标识符
     */
    @NotBlank(message = "题型代码不能为空")
    @Size(max = 20, message = "题型代码长度不能超过20个字符")
    @Column(name = "type_code", nullable = false, unique = true, length = 20)
    private String typeCode;

    /**
     * 题型名称
     */
    @NotBlank(message = "题型名称不能为空")
    @Size(max = 50, message = "题型名称长度不能超过50个字符")
    @Column(name = "type_name", nullable = false, length = 50)
    private String typeName;

    /**
     * 题型类别
     */
    @NotNull(message = "题型类别不能为空")
    @Column(name = "category", nullable = false, length = 20)
    private String category;

    /**
     * 父题型代码
     */
    @Size(max = 20, message = "父题型代码长度不能超过20个字符")
    @Column(name = "parent_type_code", length = 20)
    private String parentTypeCode;

    /**
     * 描述信息
     */
    @Size(max = 500, message = "描述信息长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 答题格式
     */
    @Size(max = 200, message = "答题格式长度不能超过200个字符")
    @Column(name = "answer_format", length = 200)
    private String answerFormat;

    /**
     * 默认分值
     */
    @Column(name = "default_score")
    @Builder.Default
    private Integer defaultScore = 1;

    /**
     * 评分规则说明
     */
    @Size(max = 500, message = "评分规则说明长度不能超过500个字符")
    @Column(name = "scoring_rules", length = 500)
    private String scoringRules;

    /**
     * 是否支持自动评分
     */
    @Column(name = "auto_gradeable")
    @Builder.Default
    private Boolean autoGradeable = true;

    /**
     * 是否需要手动评分
     */
    @Column(name = "requires_manual_grading")
    @Builder.Default
    private Boolean requiresManualGrading = false;

    /**
     * 是否默认题型
     */
    @Column(name = "is_default")
    @Builder.Default
    private Boolean isDefault = false;

    /**
     * 是否系统预定义
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = false;

    /**
     * 使用次数
     */
    @Column(name = "usage_count")
    @Builder.Default
    private Long usageCount = 0L;

    /**
     * 状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private EntityStatus status = EntityStatus.ACTIVE;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证题型代码
     */
    public boolean validateTypeCode() {
        return typeCode != null && !typeCode.trim().isEmpty();
    }

    /**
     * 验证答题格式
     */
    public boolean validateAnswerFormat() {
        return answerFormat != null && !answerFormat.trim().isEmpty();
    }

    /**
     * 判断是否为子类型
     */
    public boolean isSubType() {
        return parentTypeCode != null && !parentTypeCode.trim().isEmpty();
    }

    /**
     * 获取默认评分规则
     */
    public String getDefaultScoringRules() {
        if (scoringRules != null && !scoringRules.trim().isEmpty()) {
            return scoringRules;
        }
        return "按标准答案评分，完全正确得满分";
    }
}